/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package Model.DAO;

import Model.Pojo.TopicChoice;
import java.util.List;
import org.hibernate.Query;
import org.hibernate.Session;

/**
 *
 * @author luong
 */
public class TopicChoiceImp implements TopicChoiceDAO{

    @Override
    public boolean AjouterTopicChoice(TopicChoice topic) {
        return Model.DAO.ModelDaoLibrarie.Ajouter(topic);
    }

    @Override
    public boolean SuprimerTopicChoice(TopicChoice topic) {
        boolean Resultat=false;
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {           
            String hql= String.format("select t from TopicChoice t where t.IdTopicChoice='%s'",topic.getIdTopicChoice());
            topic= (TopicChoice) session.createQuery(hql).uniqueResult();
            session.delete(topic);
            session.beginTransaction().commit();
            Resultat=true;
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return Resultat;
    }

    @Override
    public boolean UpdateTopicChoice(TopicChoice topic) {
        return Model.DAO.ModelDaoLibrarie.Update(topic);
    }

    @Override
    public List<TopicChoice> GetListTopicChoice() {
        List<TopicChoice> listTopicChoice=null;
        Session session = Model.Util.HibernateUtil.getSessionFactory().openSession();
        try {
            String hql="from TopicChoice";
            Query query= session.createQuery(hql);
            listTopicChoice=query.list(); 
        } catch (Exception e) {
            System.err.print(e.getMessage());
        }
        finally
        {
            session.close();
        }
        return listTopicChoice;
    }
    
}
